<template>
  <el-dialog
    :title="!dataForm.adminId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="管理员姓名" prop="adminName">
      <el-input v-model="dataForm.adminName" placeholder="管理员姓名"></el-input>
    </el-form-item>
    <el-form-item label="管理员密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="管理员密码"></el-input>
    </el-form-item>
    <el-form-item label="管理员手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="管理员手机号"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="是否删除" prop="delStatus">
      <el-input v-model="dataForm.delStatus" placeholder="是否删除"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          adminId: 0,
          adminName: '',
          password: '',
          phone: '',
          createTime: '',
          updateTime: '',
          delStatus: ''
        },
        dataRule: {
          adminName: [
            { required: true, message: '管理员姓名不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '管理员密码不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '管理员手机号不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          delStatus: [
            { required: true, message: '是否删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.adminId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.adminId) {
            this.$http({
              url: this.$http.adornUrl(`/system/admin/info/${this.dataForm.adminId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.adminName = data.admin.adminName
                this.dataForm.password = data.admin.password
                this.dataForm.phone = data.admin.phone
                this.dataForm.createTime = data.admin.createTime
                this.dataForm.updateTime = data.admin.updateTime
                this.dataForm.delStatus = data.admin.delStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/system/admin/${!this.dataForm.adminId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'adminId': this.dataForm.adminId || undefined,
                'adminName': this.dataForm.adminName,
                'password': this.dataForm.password,
                'phone': this.dataForm.phone,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'delStatus': this.dataForm.delStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
